/* 
 * Copyright 2013 Imanuel Rohlfing, Dennis Wittkötter

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package management.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import management.ejb.DatabaseController;
import management.entities.Person;
import management.entities.Room;
import org.primefaces.model.DualListModel;


/**
 * Provide operations and data management for a person picklist.
 * @author Dennis Wittkötter
 */
@ManagedBean(name="roomPickList")
@SessionScoped
public class RoomPickListBean implements Serializable {
    
    /**
     * database controller
     */
    @EJB
    private DatabaseController dbC;
    
    /**
     * datasource
     */
    private DualListModel<Room> data;
    
    /**
     * the current person the rooms are related to
     */
    private Person currentPerson;
    
    
    /**
     * Sets the datasource for the picklist.
     * Persons which have access to the current room will be stored in target
     * list, all remaining persons will be stored to source list.
     */
    public RoomPickListBean() {
        data = new DualListModel<>();
    }
    
    /**
     * Sets the datasource for the picklist.
     * Rooms which the current person has access to will be stored in target
     * list, all remaining rooms will be stored to source list.
     */
    private void init() {
        List<Room> target;
        if(currentPerson.getRooms() == null ){
            target = new ArrayList<>();
        }
        else{
            target = currentPerson.getRooms();
        }
        List<Room> source = dbC.getRooms();
        if (!source.isEmpty()) {
            Iterator<Room> it = target.iterator();
            while (it.hasNext()) {
                Room room = it.next();
                while (source.contains(room)) {
                    source.remove(room);
                }
            }
        }

        data = new DualListModel<>(source, target);
    }
    
    /**
     * Sets the current person.
     * @param currentPerson     current person
     */
    public void setCurrentPerson(Person currentPerson){
        this.currentPerson = currentPerson;
        init();
    }
    
    /**
     * Provides the datasource.
     * @return the picklist source and target lists
     */
    public DualListModel<Room> getData() {
        return data;
    }

    /**
     * Sets the datasource
     * @param data  the datasource to set
     */
    public void setData(DualListModel<Room> data) {
        this.data = data;
    }
    
    /**
     * Writes the selected rooms into the current person.
     */
    public void save(){
        currentPerson.setRooms(data.getTarget());
    }
}
